package com.lanmaoly.date.api;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 时间api
 *
 * @since 1.8
 */
public class Java8Tester {

    private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        Java8Tester java8Tester = new Java8Tester();
        java8Tester.testLocalTime();
        System.out.println("-------------------------------------");
        java8Tester.mutiThread();
    }

    private void mutiThread() {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(100, 100,
                0, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(10),
                new NamedThreadFactory("java-8-test"));
        for (int i = 0; i < 100; i++) {
            poolExecutor.execute(() -> {
                TemporalAccessor parse = dateTimeFormatter.parse("2019-10-18 18:09:21");
            });
        }
        poolExecutor.shutdown();
    }

    private void testLocalTime() {
        LocalDate localDate = LocalDate.now();
        System.out.println("localDate :" + localDate);
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDate localDate1 = currentTime.toLocalDate();
        System.out.println("currentTime :" + currentTime);
        System.out.println("localTime :" + LocalTime.now());

        int year = currentTime.getYear();
        int monthValue = currentTime.getMonthValue();
        int dayOfMonth = currentTime.getDayOfMonth();
        int hour = currentTime.getHour();
        int minute = currentTime.getMinute();
        int second = currentTime.getSecond();
        this.print(year, monthValue, dayOfMonth, hour, minute, second);
        System.out.println(currentTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        System.out.println(currentTime.format(DateTimeFormatter.ISO_DATE_TIME));
    }

    private void print(Integer... integers) {
        for (Integer value : integers) {
            System.out.println(value);
        }
    }
}
